package com.mcf.xiaolinju.service.impl;

import com.mcf.xiaolinju.entity.Review;
import com.mcf.xiaolinju.mapper.ReviewMapper;
import com.mcf.xiaolinju.mapper.UserMapper;
import com.mcf.xiaolinju.service.IReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcf.xiaolinju.vo.ReviewVO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2022-02-22
 */
@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements IReviewService {
    @Resource
        ReviewMapper reviewMapper;
    @Resource
        UserMapper userMapper;
    public List<ReviewVO> getreviewmessage(int dynamicid) {
        List<ReviewVO> reviewVOS=new ArrayList<>();
        List<Review> reviews= reviewMapper.getreviewmessage(dynamicid);
        for(Review r :reviews){
            ReviewVO reviewVO=new ReviewVO();
            reviewVO.setUsername(userMapper.getUserName(r.getReviewid()));
            reviewVO.setReviewcontent(r.getReviewcontent());
            reviewVOS.add(reviewVO);
        }
        return reviewVOS;
    }

    public String insertReview(int dynamicid, String reviewcontent, int reviewid) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(reviewMapper.insertReview(dynamicid,reviewcontent,reviewid,ldt)>0){
            return "评论成功，请等待管理员的审核";
        }
        else{
            return "评论失败";
        }
    }
}
